package com.sinaapp.gavinzhang.GSpider.plugin.batchprocessing;

import com.sinaapp.gavinzhang.GSpider.VisitedWebUrlList;
import com.sinaapp.gavinzhang.GSpider.core.AbstractGWebUrlList;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;


public class FileFoundWebUrlList extends VisitedWebUrlList {
	private String fileName;
	private FileOutputStream fos;
	public FileFoundWebUrlList(String fileName) {
		super();
		this.fileName = ".Found"+fileName;
		init();
		try {
			fos = new FileOutputStream(new File(this.fileName),true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void init()
	{
		FileReader fr = null;
		BufferedReader bf = null;
		try {
			fr = new FileReader(new File(fileName));
			bf = new BufferedReader(fr);
			String url;
			while((url = bf.readLine())!= null)
			{
				this.getFoundList().add(url);
			}
		} catch ( IOException e) {
		}finally{
			if(fr != null)
			{
				try {
					fr.close();
				} catch (IOException e) {
				}
			}
			if(bf != null)
			{
				try {
					bf.close();
				} catch (IOException e) {
				}
			}
		}
		
	}
	
	@Override
	public AbstractGWebUrlList add(String webUrl) {
		try {
			fos.write((webUrl+"\n").getBytes());
		} catch (IOException e) {
		}
		return super.add(webUrl);
	}
	
	@Override
	protected void finalize() throws Throwable {
		fos.close();
		super.finalize();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
